﻿using System;
using System.Collections.Generic;
using System.Linq;

using TempEmployees.BLL.Database;

namespace TempEmployees.BLL.EntitiesExtensions
{
    public class UserRole
    {
        private int m_roles;

        public UserRole(int roles)
        {
            m_roles = roles;
        }

        public int Count { get { return Roles.Count; } }

        public List<DbConstants.Roles> Roles
        {
            get
            {
                List<DbConstants.Roles> roles = new List<DbConstants.Roles>();
                foreach (int role in Enum.GetValues(typeof(DbConstants.Roles)))
                {
                    if (role == 0) continue;
                    if (GetRole((DbConstants.Roles)role))
                    {
                        roles.Add((DbConstants.Roles)role);
                    }
                }
                return roles;
            }
        }

        public bool Secretary
        {
            get { return GetRole(DbConstants.Roles.Secretary); }
            set { SetRole(DbConstants.Roles.Secretary, value); }
        }

        public bool HeadDepartment
        {
            get { return GetRole(DbConstants.Roles.HeadDepartment); }
            set { SetRole(DbConstants.Roles.HeadDepartment, value); }
        }

        public bool Employer
        {
            get { return GetRole(DbConstants.Roles.Employer); }
            set { SetRole(DbConstants.Roles.Employer, value); }
        }

        public bool SystemAdmin
        {
            get { return GetRole(DbConstants.Roles.SystemAdmin); }
            set { SetRole(DbConstants.Roles.SystemAdmin, value); }
        }

        public bool Accountant
        {
            get { return GetRole(DbConstants.Roles.Accountant); }
            set { SetRole(DbConstants.Roles.Accountant, value); }
        }

        public bool GetRole(DbConstants.Roles role)
        {
            return (m_roles & (int)role) == (int)role;
        }

        public void SetRole(DbConstants.Roles role, bool set)
        {
            if (set)
                m_roles |= (int)role;
            else
                m_roles &= (0xFFFF ^ (int)role);
        }
    }
}